package com.baibei.product.quotation;

import android.content.Context;
import android.support.v4.content.res.ResourcesCompat;
import android.view.LayoutInflater;
import android.view.ViewGroup;

import com.baibei.basic.BasicRecyclerViewAdapter;
import com.baibei.model.Area;
import com.baibei.model.RankingInfo;
import com.baibei.module.AreaHelper;
import com.baibei.product.R;
import com.blankj.utilcode.utils.Utils;
import com.bumptech.glide.Glide;
import com.rae.swift.Rx;

import java.math.BigDecimal;
import java.math.RoundingMode;


/**
 * 排行榜
 * Created by ChenRui on 2017/8/9 0009 15:22.
 */
public class RankingAdapter extends BasicRecyclerViewAdapter<RankingInfo, RankingViewHolder> {
    private Context context;
    public RankingAdapter(Context context) {
        this.context = context;
    }

    @Override
    protected RankingViewHolder onCreateViewHolder(LayoutInflater layoutInflater, ViewGroup parent, int i) {
        return new RankingViewHolder(inflater(R.layout.item_ranking_quotation, parent));
    }
    @Override
    protected void onBindViewHolder(RankingViewHolder holder, int i, RankingInfo info) {
        holder.getTitleView().setText(info.getName());
        double scope = info.getScope();
        int roseBgId;
        int textColorResource;
        String signal = "";
        // 上涨
        if (scope > 0) {
            roseBgId = R.drawable.btn_trade_up;
            textColorResource = R.color.buyColor;
            signal = "+";
        }
        // 下跌
        else if (scope < 0) {
            roseBgId = R.drawable.btn_trade_down;
            textColorResource = R.color.sellColor;
        }
        // 平稳
        else {
            roseBgId = R.drawable.btn_trade_neutral;
            textColorResource = R.color.textPrimary;
        }

        holder.getTvPayPrice().setText(info.getPrice()+"");
        holder.getLlRose().setBackgroundResource(roseBgId);
        holder.getPriceView().setText(Rx.formatPrice(info.getLastPrice()));
        holder.getPriceView().setTextColor(ResourcesCompat.getColor(Utils.getContext().getResources(), textColorResource, null));
        holder.getRoseView().setText(String.format("%s%s%%", signal, new BigDecimal(info.getScopePercent() * 100).setScale(2, RoundingMode.DOWN)));

        //demo
//        holder.getAreaFlagView().setImageResource(AreaHelper.getImageResource(Area.valueOf(info.getArea())));
        holder.getTvRose().setText((new BigDecimal(info.getLastPrice()-info.getClose()).setScale(2,BigDecimal.ROUND_HALF_UP))+"");
        holder.getTimeView().setText(info.getSaleTips());
        if (context!=null){
            Glide.with(context).load(info.getProductPic()).into(holder.getAreaFlagView());
        }
    }
}
